<?php
/**
 * 老师会员中心 首页以及其他琐碎操作方法
 * @author      HJC <hujianchuang@haowj.com>
 * @update      2014.07.06
 * @since       Version 1.0
 * 
 */
class IndexAction extends BaseAction
{
    /*
     * 老师帮学生完成简历
     */
	public function resume()
	{
	    $user_id = $this->_get('uid');
        if(!$user_id) die('error data!');
	    //如果老师没有这个学校的资料 复制一份给老师
	    $this->copyInfo($user_id);
	    $teacher_id = $this->userInfo['id'];
	    $result = D('Practice')->where("member_id=".$user_id." and member_type=2 and teacher_id=".$teacher_id."  and practice_type=".C('PACKPRACTICE'))->select();
        if($result)
        {
            foreach ($result as $k=>$v)
            {
                $pct += $v['precentum'];
                $ret[$v['step']] = $v;
                $ret[$v['step']]['value'] = json_decode($v['value'],true);
                $ret[$v['step']]['count'] = count($ret[$v['step']]['value']);
            }
        }
        $this->assign('user_id',$user_id);
        $this->assign('ret',$ret);
	    $this->display();
	}
	public function copyInfo($user_id)
	{
	    $result = D('Practice')->where("member_id=".$user_id." and member_type=1  and practice_type=".C('PACKPRACTICE'))->select();
	    if(!$result) return;
	    foreach ($result as $k=>$v)
	    {
	       $info = D('Practice')->where("member_id=".$user_id." and member_type=2 and teacher_id=".$this->userInfo['id']." and step=".$v['step']."  and practice_type=".C('PACKPRACTICE'))->find();
	       if(!$info)
	       {
	           $info = D('Practice')->where("member_id=".$user_id." and member_type=1 and step=".$v['step']."  and practice_type=".C('PACKPRACTICE'))->find();
	           if($info)
	           {
		           unset($info['id']);
		           $info['member_type'] = 2;
		           $info['edit_num'] = 0;
		           $info['teacher_id'] = $this->userInfo['id'];
		           D('Practice')->add($info);
	           }
	       }   
	    }
	}
    public function postResume()
    {
        $teacher_id = $this->userInfo['id'];
        if(!$teacher_id||!IS_AJAX) $this->ajaxReturn('','非法请求！',0);
        $post = $this->_post();
        $user_id = $post['user_id'];
        $data['step'] = $post['step'];
        $data['precentum'] = $post['precentum'];
        $num = $post['num'];
        
        unset($post['step']);unset($post['precentum']);
        $id = D('Practice')->where("member_id=".$user_id." and member_type=2 and teacher_id=".$teacher_id." and step=".$data['step']." and practice_type=".C('PACKPRACTICE'))->getField('id');
       
        if(!empty($num)) 
        {
            if($id)
            {
                $result = D('Practice')->where("id=".$id)->find();
                $value = json_decode($result['value'],true);
            }
            $value[$num] = $post;
           
            $data['value'] = json_encode($value);
        }
        else 
        {
            if($data['step']==5)
            {
                $count = count($post);
                for($i=1;$i<=($count/2);$i++)
                {
                    $ret[$i]['lang'] = $post['lang'.$i];
                    $ret[$i]['level'] = $post['level'.$i];
                }
                $post = $ret;
               
            }
            $data['value'] = json_encode($post);
        }
        $data['practice_type'] = C('PACKPRACTICE');
        $data['member_type'] = 2;
        $data['member_id'] = $user_id;
        $data['teacher_id'] = $teacher_id;
        if($id)
        {
            D('Practice')->where("id=".$id)->data($data)->save();
        }
        else 
        {
            $id = D('Practice')->data($data)->add();
        }
        //更新编辑次数
        D('Practice')->where("id=".$id)->setInc('edit_num',1);
        $this->ajaxReturn('','添加成功！！',1);
    }
    public function delResume()
    {
        $teacher_id = $this->userInfo['id'];
        $user_id = $this->_post('user_id');
        if(!$user_id||!IS_AJAX) $this->ajaxReturn('','非法请求！',0);
        $post = $this->_post();
        $data['step'] = $post['step'];
       
        $num = $post['num'];
        $id = D('Practice')->where("member_id=".$user_id." and member_type=2 and step=".$data['step']." and teacher_id=".$teacher_id." and practice_type=".C('PACKPRACTICE'))->getField('id');
       
        if($id)
        {
            $result = D('Practice')->where("id=".$id)->find();
            $value = json_decode($result['value'],true);
        }
        unset($value[$num]);
           
        $data['value'] = json_encode($value);
        if(empty($value)) D('Practice')->where("id=".$id)->delete();
        else D('Practice')->where("id=".$id)->data($data)->save();
        $this->ajaxReturn('','编辑成功！！',1);
    }
    public function  s2html()
    {
        $num = $this->_post('num');
        $step = $this->_post('step');
        $teacher_id = $this->userInfo['id'];
        $user_id = $this->_post('user_id');
        if(!$user_id||!IS_AJAX) $this->ajaxReturn('','非法请求！',0);
        $result = D('Practice')->where("member_id=".$user_id." and member_type=2 and step=".$step." and teacher_id=".$teacher_id." and practice_type=".C('PACKPRACTICE'))->find();
        $value = json_decode($result['value'],true);
        $ret = $value[$num];
        for($i=2014;$i>=1960;$i--)
        {
            $year .= '<li class="lieli">'.$i.'</li>';
        }
        for($j=1;$j<=12;$j++){
            $month.= '<li class="lieli">'.$j.'</li>';
        }
        
        if($ret['month1']){
            $month1=$ret['month1'];
        }else{
            $month1='month';
        }
        if($ret['month2']){
            $month2=$ret['month2'];
        }else{
            $month2='month';
        }
        if($ret['year11']){
            $year1=$ret['year11'];
        }else{
            $year1='year';
        }
        if($ret['year22']){
            $year2=$ret['year22'];
        }else{
            $year2='year';
        }
        $str3 = '<form name="form3-'.$num.'" id="form3-'.$num.'">
                    <ul>
                        <li class="box_title"><span style="float: left">School<em style="color: #ff6155">*</em></span><span style="float:left;margin-left: 20px;padding-left: 5px;" class="bg titip none">Please fill in school</span></li>
                        <li><input type="text" id="school" name="school" class="put3" value="'.$ret['school'].'"    ></li>
                        <li class="box_title">Dates Attended</li>
                        <li style="width:530px; height:30px; line-height:30px;">
                            <div class="sel" >
                                <p class="selinput" >'.$ret['year1'].'</p>
                                <p class="selicon "></p>
                                
                                <ul class="lieb" style="z-index:10">
                                    <input name="year1" value="'.$ret['year1'].'"  id="year" type="hidden" />
                                   
                                    '.$year.'
                                    
                                </ul>
                                    
                                
                            </div>
                            <div class="fleft">-</div>
                            <div class="sel" >
                                <p class="selinput" >'.$ret['year2'].'</p>
                                <p class="selicon "></p>
                                
                                <ul class="lieb" style="z-index:10">
                                    <input name="year2" value="'.$ret['year2'].'"  id="year" type="hidden" />
                                  
                                    '.$year.'
                                  
                                </ul>
                                    
                                
                            </div>
                            <div class="fleft" style="margin-left:5px; display:inline;">Or expected graduation year</div>
                        </li>
                        <li class="box_title">Degree<em style="color: #ff6155">*</em></li>
                        <li >
                            <div style="width:500px;height:30px;">
                            <div class="sel"  >
                                    <p class="selinput" >'.$ret['degree'].'</p>
                                    <p class="selicon "></p>
                                    
                                    <ul class="lieb" style="z-index:5">
                                        <input type="hidden" id="year"  name="degree"  value="'.$ret['degree'].'">
                                        
                                        <li class="lieli">Postdoctor</li>
                                        <li class="lieli">Doctorate</li>
                                        <li class="lieli">MBA/EMBA</li>
                                        <li class="lieli">Master</li>
                                        <li class="lieli">Bachelor</li>
                                        <li class="lieli">Junior College</li>
                                        <li class="lieli">Special School</li>
                                        <li class="lieli">Technical School</li>
                                        <li class="lieli">Senior High School</li>
                                        <li class="lieli">Junior High School</li>
                                    </ul>
                                        
                                    
                            </div>
                            </div>
                        </li>
                        <li class="box_title">Major</li>
                        <li><input type="text" id="study" name="study" class="put3" value="'.$ret['study'].'"></li>
                        <li class="box_title">Activities and Societies</li>
                        <li><textarea id="societies" name="societies" class="put3 pad_10" style="width:485px;" >'.$ret['societies'].'</textarea></li>
                        <li class="box_title">Examples: English Debate Society, voluntary organization, Chorus</li>
                        <li class="box_title">Description</li>
                        <li><textarea id="description" name="description" class="put3 pad_10"  style="width:485px;" >'.$ret['description'].'</textarea></li>
                        <li class="center"><span  style="cursor: pointer" id="pereonal" step="3" num='.$num.' ptc="25" class="bg bc"></span><span style="cursor: pointer" num='.$num.' step="3" class="bg qx fhqx"></span></li>
                    </ul>
                    </form>';
        $str4 = '<form name="form4-'.$num.'" id="form4-'.$num.'">
                            <table>
                                <tr>
                                    <td class="right">Project Name</td>
                                    <td><input type="text" name="project" class="put4" id="project" value="'.$ret['project'].'" ></td>
                                </tr>
    
                                <tr>
                                    <td class="right">Company Name<em style="color: #ff6155">*</em></td>
                                    <td><input type="text" name="company" class="put4" id="company" value="'.$ret['company'].'"></td>
                                </tr>
                                <tr>
                                    <td class="right">Position<em style="color: #ff6155">*</em></td>
                                    <td><input type="text" name="position" class="put4" id="position" value="'.$ret['position'].'"></td>
                                </tr>
                                <tr>
                                    <td class="right">Period<em style="color: #ff6155">*</em></td>
                                    <td>
                                        <div class="sell" >
                                <p class="sellinput" >'.$month1.'</p>
                                <p class="sellicon "></p>
                                
                                <ul class="llieb" style="z-index:10">
                                    <input name="month1" value="'.$ret['month1'].'"  id="year" type="hidden" style="width:50px;"/>
                                   
                                    '.$month.'
                                    
                                </ul>
                                    
                                
                            </div>
                            <div class="sell" >
                                <p class="sellinput" >'.$year1.'</p>
                                <p class="sellicon "></p>
                                
                                <ul class="llieb" style="z-index:10">
                                    <input name="year11" value="'.$ret['year1'].'"  id="year" type="hidden" style="width:50px;"/>
                                   
                                    '.$year.'
                                    
                                </ul>
                                    
                                
                            </div>
                            <div class="fleft">-</div>
                            <div class="sell"  >
                                <p class="sellinput" >'.$month2.'</p>
                                <p class="sellicon "></p>
                                
                                <ul class="llieb" style="z-index:10">
                                    <input name="month2" value="'.$ret['month2'].'"  id="year" type="hidden" />
                                  
                                    '.$month.'
                                  
                                </ul>
                                    
                                
                            </div>
                            <div class="sell" >
                                <p class="sellinput" >'.$year2.'</p>
                                <p class="sellicon "></p>
                                
                                <ul class="llieb" style="z-index:10">
                                    <input name="year22" value="'.$ret['year2'].'"  id="year" type="hidden" />
                                  
                                    '.$year.'
                                  
                                </ul>
                                    
                                
                            </div>
                                        </td>
                                </tr>
                                <tr>
                                    <td class="right" valign="top" style="padding-top: 10px;">Description<em style="color: #ff6155">*</em></td>
                                    <td><textarea name="description1" class="put4" id="description1" >'.$ret['description1'].'</textarea></td>
                                </tr>
                                <tr>
                                    <td class="right" valign="top" style="padding-top: 10px;">Responsibilities</td>
                                    <td><textarea name="responsibilities" class="put4" id="responsibilities" >'.$ret['responsibilities'].'</textarea></td>
                                </tr>
                                <tr>
                                    <td class="right" valign="top" style="padding-top: 10px;">Achievement</td>
                                    <td><textarea name="achievement" class="put4" id="achievement" >'.$ret['achievement'].'</textarea></td>
                                </tr>
                                <tr>
                                    <td colspan="2" class="center"><span  id="pereonal" num='.$num.' style="cursor: pointer" step='.$step.' ptc="25" class="bg bc"></span><span step="4" style="cursor: pointer" class="bg qx"></span></td>
                                </tr>
                            </table>
                        </form>';
        if($step==3) $info = $str3;
        if($step==4) $info = $str4;
        if($step==5) $info = $str5;
        $this->ajaxReturn('',$info,1);
    }
    public function postLang()
    {
        $num = $this->_post('anum');
        $sy = 100-$num;
        $teacher_id = $this->userInfo['id'];
        $user_id = $this->_post('user_id');
        if(!$user_id||!IS_AJAX) $this->ajaxReturn('','非法请求！',0);
        $str = '<li style="width:530px; height:30px; line-height:30px;">
                            <div class="sel" style="width:100px;" >
                                <p class="selinput" style="width:70px;" >English</p>
                                <p class="selicon "></p>
                                
                                <ul class="lieb" style="width:100px; height:100px; z-index:'.$sy.';">
                                    <input name="lang"  value="English"  id="year" type="hidden" />
                                    
                                    <li class="lieli" >English</li>
                                    <li class="lieli" >Mandarin</li>
                                    <li class="lieli" >Cantonese</li>
                                    <li class="lieli" >Others</li>
                                </ul>
                                    
                                
                            </div>
                            <div class="fleft">-</div>
                            <div class="sel" style="width:220px">
                                <p class="selinput" style="width:190px;">Proficiency...</p>
                                <p class="selicon "></p>
                                
                                <ul class="lieb" style="width:220px; height:150px;z-index:'.$sy.';">
                                    <input name="level" value="Proficiency"  id="year" type="hidden" />
                                    
                                    
                                    <li class="lieli" style="width:200px;">Elementary proficiency</li>
                                    <li class="lieli" style="width:200px;">Limited working proficiency</li>
                                    <li class="lieli" style="width:200px;">Professional working proficiency</li>
                                    <li class="lieli" style="width:200px;">Full professional proficiency</li>
                                    <li class="lieli" style="width:200px;">Native or bilingual proficiency</li>
                                </ul>
                                    
                                
                            </div>
                            <div class="fleft bg dellang" style="background-position:-320px -40px; width:30px; height:30px; margin-top:2px; margin-left:3px; display:inline; cursor:pointer;"></div>
                        </li>';
        $this->ajaxReturn('',$str,1);
    }
}